package com.teacher.hong.service.impl;

import com.teacher.hong.bean.Page;
import com.teacher.hong.dao.ScoreDao;
import com.teacher.hong.dao.StudentDao;
import com.teacher.hong.entity.BorrowRecord;
import com.teacher.hong.entity.Student;
import com.teacher.hong.service.IBorrowRecordService;
import com.teacher.hong.service.IStudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.SQLException;

/**
 * 学生服务实现类
 */
@Slf4j
@Service("studentService")
public class StudentServiceImpl implements IStudentService {

    public StudentServiceImpl(){
        log.debug("执行了StudentServiceImpl的构造函数。。。。。。。。。。。。。。。。。。。。。。");
    }

    @Resource
    StudentDao studentDao;
    @Resource
    ScoreDao scoreDao;

//    public void setStudentDao(StudentDao studentDao) {
//        this.studentDao = studentDao;
//        System.out.println("执行了StudentServiceImpl的setStudentDao函数...");
//    }
//
//    public void setScoreDao(ScoreDao scoreDao) {
//        this.scoreDao = scoreDao;
//        System.out.println("执行了StudentServiceImpl的setScoreDao函数...");
//    }

    @Override
    public Page<Student> page(int pageIndex, Integer clazzId, String name) {
        //这里，我们需要返回查询的数据，同时还要条数、页数等信息
        Page<Student> studentPage = new Page<>(pageIndex, 5);
        try {
            studentPage.setRecords(studentDao.select(pageIndex, 5, clazzId, name));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        try {
            studentPage.setTotal(studentDao.selectCount(clazzId, name));
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return studentPage;
    }

    @Override
    public boolean save(Student student) {
        try {
            if (student.getId() != 0 && studentDao.selectCountById(student.getId()) == 0) {
                System.out.println("该班ID不存在");
                return false;
            }
            if (student.getId() == 0) {
                student.setMoney(100);  //当创建新的学生时，初始余额100元
                return studentDao.insert(student) > 0;
            } else {
                return studentDao.update(student) > 0;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean remove(int id) {
        try {
            if (studentDao.selectCountById(id) == 0) {
                System.out.println("学生ID不存在");
                return false;
            }
            if (scoreDao.selectCountByStudentId(id) > 0) {
                System.out.println("该学生存在关联的成绩数据");
                return false;
            }
            return studentDao.delete(id) > 0;
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            return false;
        }
    }

    @Resource
    IBorrowRecordService borrowRecordService;

//    @Resource
//    TransactionDefinition transactionDefinition;    //配置事务定义器
//    @Resource
//    DataSourceTransactionManager transactionManager;    //配置事务管理器

    @Override
    @Transactional
    public boolean borrowMoney(int student1Id, int student2Id, int money) throws SQLException {

//        //创建一个事务状态对象
//        TransactionStatus transactionStatus = transactionManager.getTransaction(transactionDefinition);
//
//        try {

            borrowRecordService.record(new BorrowRecord(student1Id, student2Id, money));    //保存借钱记录

            int result2 = studentDao.updateMoney(student2Id, -money); //加钱
            int result1 = studentDao.updateMoney(student1Id, money); //减钱

//            //如果上面的若干代码均未报错，则提交事务
//            transactionManager.commit(transactionStatus);
            System.out.println("提交事务...");

            //增加一个 借钱记录表数据的保存
            return result1 > 0 && result2 > 0;

//        } catch (Exception exception) {
//            //如果上面的若干代码有一处报错，则try里面的代码执行的结果全数回滚
//            transactionManager.rollback(transactionStatus);
//            System.out.println("报错了，事务回滚...");
//            exception.printStackTrace();
//            return false;
//        }
    }
}
